package com.meixiang.activity.account.storesmanager;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.activity.homes.cosmeticTraining.CosmeticTrainingActivity;
import com.meixiang.activity.homes.cosmeticTraining.CosmeticTrainingDetailActivity;
import com.meixiang.adapter.home.CosmeticTrainingAdapter;
import com.meixiang.entity.training.Training;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.inteface.ITextChangedCallback;
import com.meixiang.main.BaseActivity;
import com.meixiang.tool.Tool;
import com.meixiang.util.AbJsonUtil;
import com.meixiang.view.ClearEditText;
import com.meixiang.view.SearchEditText;
import com.meixiang.view.swipetoloadlayout.base.OnLoadMoreListener;
import com.meixiang.view.swipetoloadlayout.base.OnRefreshListener;
import com.meixiang.view.swipetoloadlayout.base.SwipeToLoadLayout;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ydc on 2016/9/21.
 */

public class StoresListActivity extends BaseActivity implements ITextChangedCallback {



    @Bind(R.id.swipe_target)
    RecyclerView mRecycler;
    @Bind(R.id.refresh)
    SwipeToLoadLayout swipeRefresh;

    @Bind(R.id.left_image_btn)
    ImageView left_image_btn;

    @Bind(R.id.iv_collect)
    ImageView iv_collect;


    @Bind(R.id.cet_input_name)
    SearchEditText cet_input_name;


    @Bind(R.id.tv_cancel)
    TextView tv_cancel;



    private CosmeticTrainingAdapter mAdapter;
    private int pageNo = 1, pageSize = 10,totalPage=0;
    private boolean isRefresh = true;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_storesmanage_storeslist);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        //setTitle("门店列表");

    }

    @Override
    protected void initView() {
        mAdapter=new CosmeticTrainingAdapter(context);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecycler.setLayoutManager(linearLayoutManager);
        mRecycler.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new CosmeticTrainingAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View view) {
                if (Tool.isFastDoubleClick()) return;
                Intent intent=new Intent(StoresListActivity.this,StoresDetailActivity.class);
                intent.putExtra("goodsId",mAdapter.getData().get(position).getGoodsId());
                startActivity(intent);
            }
        });
        setEvent();


        cet_input_name.initITextChangedCallback(StoresListActivity.this);
    }

    @Override
    protected void setData() {
        getData();

    }

    public void onRefresh(){
        swipeRefresh.setRefreshing(true);
    }

    private void onCompleteRefresh() {
        if (swipeRefresh != null) {
            if (isRefresh == true) {
                swipeRefresh.setRefreshing(false);
            } else {
                swipeRefresh.setLoadingMore(false);
            }
        }
    }

    private void setEvent() {
        swipeRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                isRefresh=false;
                pageNo++;
                if(pageNo>totalPage){
                    swipeRefresh.setLoadingMore(false);
                    return;
                }
                getData();

            }
        });
        swipeRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                isRefresh=true;
                pageNo = 1;
                getData();
            }
        });
    }

    private void getData(){
        status.showLoading();
        HttpUtils.isAnalog=false;
        String url= Config.TRAINING_FOR_GET_LIST;
        HttpParams params=new HttpParams();
        params.put("appoitmentType","5");
        params.put("pageSize", pageSize + "");
        params.put("pageNo", pageNo + "");
        HttpUtils.post(url, params, new HttpCallBack(this) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                onCompleteRefresh();
                status.removeView();
                totalPage=result.optInt("totalPage");
                if(totalPage==0){
                    status.showNoData("暂无数据");
                }
                List<Training> training= AbJsonUtil.fromJson(result.optJSONArray("list").toString(),new TypeToken<ArrayList<Training>>(){});
                if(isRefresh==true&&mAdapter.getItemCount()>0){
                    mAdapter.clear();
                }
                mAdapter.addAll(training);
            }

            @Override
            public void onError(String code, String message) {
                onCompleteRefresh();
                status.removeView();
                status.showNoNewWork(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getData();
                    }
                });
                Tool.showTextToast(context,message);

            }
        });
    }

    @OnClick({R.id.left_image_btn,R.id.iv_collect,R.id.tv_cancel})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.left_image_btn:
                finish();
                break;
            case R.id.iv_collect:
                Tool.showTextToast(context,"选择城市");
                break;

            case R.id.tv_cancel:
                finish();
                break;

        }

    }


    @Override
    public void textChangedCallback() {
        Tool.showTextToast(context,"测试回调接口");
        tv_cancel.setVisibility(View.VISIBLE);
    }
}
